package com.qf.data.api;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.qf.common.bean.R;
import com.qf.data.service.HeartBeatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 设备是否在线统计查询
 * @author 千锋健哥
 */
@RestController
@RequestMapping("/data/heartBeat")
public class HeartBeatController {

    @Autowired
    private HeartBeatService heartBeatService;

    /**
     * 查询指定设备是否在线
     * @return
     */
    @GetMapping("/deviceState/{productKey}/{deviceKey}")
    public R<Boolean> deviceState(@PathVariable(value = "productKey") String productKey,
                                  @PathVariable(value = "deviceKey") String deviceKey) {
        if (StringUtils.isEmpty(productKey)) {
            return R.fail("产品key不可以为空!");
        }
        if (StringUtils.isEmpty(deviceKey)) {
            return R.fail("设备key不可以为空!");
        }
        Boolean flag = heartBeatService.deviceState(productKey, deviceKey);
        return R.ok(flag);
    }

    /**
     * 统计产品下的在线设备数量
     * @param productKey
     * @return
     */
    @GetMapping("/deviceState/{productKey}")
    public R<Integer> onLineCount(@PathVariable(value = "productKey") String productKey) {
        if (StringUtils.isEmpty(productKey)) {
            return R.fail("产品key不可以为空!");
        }
        Integer count = heartBeatService.onLineCount(productKey);
        return R.ok(count);
    }

}
